package by.demchenko.sample.webapp.page.home.teachers;

import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.datamodel.enums.ScheduleStatus;
import by.demchenko.sample.services.ScheduleService;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import by.demchenko.sample.webapp.page.home.BaseLayout;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;

import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.List;


public class TeacherCoursesPage extends BaseLayout {

    @Inject
    ScheduleService scheduleService;

    public TeacherCoursesPage() {
        final List<Schedule> schedules = scheduleService.getByTeacher(BasicAuthenticationSession.get().getUser());

        add(new ListView<Schedule>("courseList", schedules) {

            @Override
            protected void populateItem(ListItem<Schedule> item) {
                Schedule schedule = item.getModelObject();
                Link link = new Link("course", Model.of(schedule.getCourse())) {
                    @Override
                    public void onClick() {
                        setResponsePage(new GradesSetPage(schedule));
                    }
                };
                item.add(link);
                link.add(new Label("courseName", Model.of(schedule.getCourse().getName())));
                SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
                Label startDate = new Label("startDate", Model.of(DATE_FORMAT.format(schedule.getStartDate())));
                item.add(startDate);
                if (schedule.getStatus() != ScheduleStatus.CLOSED) {
                    link.setVisible(false);
                    startDate.setVisible(false);
                }
            }
        });
    }
}
